from keeps.settings import SECRET_KEY
import jwt
from datetime import timedelta
from datetime import datetime


class MJwt():
    def __init__(self):
        self.secret = SECRET_KEY

    def jwt_encode(self, data):
        return jwt.encode(data, self.secret, algorithm="HS256")


    # 解密
    def jwt_decode(self, token):
        try:
            payload = jwt.decode(token, self.secret, algorithms=["HS256"])
            # 可以在这里添加额外的验证逻辑，例如检查userid或roleid的有效性
            return payload
        except jwt.ExpiredSignatureError:
            raise Exception("Token has expired")
        except jwt.InvalidTokenError:
            raise Exception("Invalid token")

myjwt=MJwt()